import java.util.List;

public class StatementGenerator {

    public static final String DIVIDE = "\n-----------------------------------";
    public static final String FOOTER = "\n===================================";

    public static String printStatement(PromotionOrder promotionOrder) {
        StringBuilder result = new StringBuilder();
        result.append("============= Order details =============");
        result.append(formatOrderItems(promotionOrder.orderItems));
        result.append(DIVIDE);
        result.append(promotionOrder.bestPromotionResult.formatStatement());
        result.append(formatTotalAmount(promotionOrder.amount));
        result.append(FOOTER);

        return result.toString();
    }

    private static String formatTotalAmount(double amount) {
        return String.format("\nTotal：%.0f yuan", amount);
    }

    private static String formatOrderItems(List<OrderItem> orderItems) {
        return orderItems.stream()
                .map(StatementGenerator::formatOrderItem)
                .reduce((pre, cur) -> pre + cur)
                .orElse("");
    }

    private static String formatOrderItem(OrderItem orderItem) {
        return String.format("\n%s x %d = %.0f yuan", orderItem.getName(), orderItem.getCount(), orderItem.getAmount());
    }
}
